layui.use(['form','layer','table','jquery','upload'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;

    var userId = localStorage.getItem("userId");
    var account = localStorage.getItem("account");
    var userName = localStorage.getItem("userName");
    var count = 0;
    var flag = true;

    numCountAll("1",$("#span2"));
    numCountAll("2",$("#span3"));
    numCountAll("3",$("#span4"));
    function numCountAll(status,name) {
        if(flag){
            var userType ="1";
        }else {
            var userType ="2";
        }
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"userType":userType,"type":2,"status":status},
            url:"/courseComment/CountCourseNum",
            success:function (data) {
                if(data.data){
                    name.text(data.data);
                }else{
                    name.text(0);
                }
                count+=data.data;
                $("#span1").text(count);
            }
        })
    };
    /*全部评论*/
    $("#quanbu").click(function () {
        show("quanbu");
        end("zhengchang");
        end("yilanjie");
        end("yishanchu");
        if(flag){
            selectBeforeTable("/courseComment/getBeforeTable?status=''"+"&type="+2)
            numCount("");
        }else{
            getAfterTable("/courseComment/getAfterTable?status="+status+"&type="+2);
        }
    });
    /*正常*/
    $("#zhengchang").click(function () {
        end("quanbu");
        show("zhengchang");
        end("yilanjie");
        end("yishanchu");
        var inp = $("#in").val();
        if(flag){
            selectBeforeTable("/courseComment/getBeforeTable?status="+1+"&type="+2)
            BeforeCount(1);
        }else{
            getAfterTable("/courseComment/getAfterTable?status="+1+"&type="+2);
        }
    });
    /*已拦截*/
    $("#yilanjie").click(function () {
        end("quanbu");
        end("zhengchang");
        show("yilanjie");
        end("yishanchu");
        if(flag){
            selectBeforeTable("/courseComment/getBeforeTable?status="+2+"&type="+2)
            BeforeCount(2);
        }else{
            getAfterTable("/courseComment/getAfterTable?status="+2+"&type="+2);
        }
    });
    /*已删除*/
    $("#yishanchu").click(function () {
        end("quanbu");
        end("zhengchang");
        end("yilanjie");
        show("yishanchu");
        if(flag){
            selectBeforeTable("/courseComment/getBeforeTable?status="+3+"&type="+2)
        }else{
            getAfterTable("/courseComment/getAfterTable?status="+3+"&type="+2);
        }
    });

    numCount("");
    /*数字表格数据*/
    function numCount(status) {
        if(flag){
            var userType ="1";
        }else {
            var userType ="2";
        }
        var commentDate = new Date();
        commentDate = getTime(commentDate);
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"type":2,"status":status,"userType":userType,"commentDate":commentDate},
            url:"/courseComment/countCourseComment",
            success:function (data) {
                $("#sum").empty();
                $("#sum").append(
                    "<th>"+data.data[0]+"</th>"
                    +"<th>"+data.data[1]+"</th>"
                    +"<th>"+data.data[2]+"</th>"
                    +"<th>"+data.data[3]+"</th>"
                );
            }
        })
    };
    var status = "";
    selectBeforeTable("/courseComment/getBeforeTable?status="+status+"&type="+2)
    function selectBeforeTable(url) {
        table.render({
            elem:"#root",
            url:url,
            defaultToolbar: ['','',''],
            page:true,
            limit:10,
            limits:[10,15,20],
            size: 'lg',
            cols:[[
                {type:"numbers",title:"序号",align:"center"},
                {field:"account",title:"评论人账号",align:"center"},
                {field:"nickname",title:"评论人昵称",align:"center"},
                {field:"courseName",title:"所属课程",align:"center"},
                {field:"commentDate",title:"发言时间",align:"center"},
                {field:"content",title:"发言内容",align:"center"},
                {field:"status",title:"状态",align:"center",templet:function (data) {
                        if(data.status=="1"){
                            return "正常";
                        }else if(data.status=="2"){
                            return "已拦截";
                        }else {
                            return "已删除";
                        }
                    }},
                {title:"操作",align:"center",templet:function (data) {
                        if(data.status=="1"){
                            return "<div>"
                                +"<a href='javascript:chakan("+data.id+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                +"<a href='javascript:lanjie("+data.id+");' style='color: #2F4056;margin-right: 10px;'>拦截</a>"
                                +"<a href='javascript:shanchu("+data.id+");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                +"</div>";
                        }else if(data.status=="2"){
                            return "<div>"
                                + "<a href='javascript:chakan(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "<a href='javascript:shanchu(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                + "</div>";
                        }else if(data.status=="3"){
                            return "<div>"
                                + "<a href='javascript:chakan(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "</div>";
                        }
                    }}
            ]]
        })
    };
    /*查看*/
    chakan = function chakan(id){
        //console.log(courseId);
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"id":id},
            url:"/courseComment/getBeforeComment",
            success:function (data) {
                BeforeLayer(data.data);
            }
        });
    };
    function BeforeLayer(data,content){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看课程评论",
            content:"/newcomm/comment-course-see.html",
            area:["800px","600px"],
            btn:["关闭"],
            yes:function(index){
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                localStorage.setItem("commodityId", data.commodityId);
                obj.find("#courseName").text(data.courseName);
                obj.find("#createDate").text(data.createDate);
                obj.find("#trueName").text(data.trueName);
                obj.find("#courseNprice").text(data.courseNprice);
                obj.find("#coursePrimary").text(data.coursePrimary);
                obj.find("#title").text(data.title);
                obj.find("#recommend").text(data.recommend);
                if(data.courseCstatus=="2"){
                    var courseStatus = "上架";
                }else {
                    var courseStatus =  "未上架";
                }
                obj.find("#courseStatus").text(courseStatus);

                if(data.courseCstatus=="3"){
                    var courseCstatus = "未审核";
                }else if(data.courseCstatus=="4"){
                    var courseCstatus = "已通过";
                }else if(data.courseCstatus=="5"){
                    var courseCstatus = "审核中";
                }else{
                    var courseCstatus = "审核失败";
                }

                obj.find("#courseCstatus").text(courseCstatus);
                obj.find("#courseTime").text(data.courseTime);
                obj.find("#likeNum").text(data.likeNum);
                obj.find("#shareNum").text(data.shareNum);
                obj.find("#collectNum").text(data.collectNum);
                obj.find("#lookNum").text(data.lookNum);
                obj.find("#saleNum").text(data.saleNum);
                obj.find("#courseUrl").attr("src", 'http://'+data.courseUrl);
                if(data.courseInfo==null){
                    var courseInfo = "";
                }else{
                    var courseInfo = data.courseInfo;
                }
                obj.find("#courseInfo").text(courseInfo);
                obj.find("#userId").val(userId);
                obj.find("#trueNamee").val(userName);
                obj.find("#account").val(account);
            }
        })
    };
    /*拦截一条评论*/
    lanjie = function lanjie(id){
        layer.confirm("是否确定拦截这条评论",{
                icon:3,title:"提示"
            },
            function (index) {
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:{"id":id,"status":2},
                    url:"/comment/delGoodsComment",
                    success:function (data) {
                        alert("拦截成功");
                        location.reload();
                    },error:function () {
                        alert("拦截失败");
                        location.reload();
                    }
                })
            }
        )
    }
    /*删除一条评论*/
    shanchu = function shanchu(id){
        layer.confirm("是否确定删除这条评论",{
                icon:3,title:"提示"
            },
            function (index) {
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:{"id":id,"status":3},
                    url:"/comment/delGoodsComment",
                    success:function (data) {
                        alert("删除成功");
                        location.reload();
                    },error:function () {
                        alert("删除失败");
                        location.reload();
                    }
                })
            }
        )
    };
    /*后台评论表*/
    function getAfterTable(url) {
        table.render({
            elem:"#root",
            url:url,
            defaultToolbar: ['','',''],
            page:true,
            limit:10,
            limits:[10,15,20],
            size: 'lg',
            cols:[[
                {type:"numbers",title:"序号",align:"center"},
                {field:"account",title:"评论人账号",align:"center"},
                {field:"trueName",title:"评论人昵称",align:"center"},
                {field:"courseName",title:"所属课程",align:"center"},
                {field:"commentDate",title:"发言时间",align:"center"},
                {field:"content",title:"发言内容",align:"center"},
                {field:"status",title:"状态",align:"center",templet:function (data) {
                        if(data.status=="1"){
                            return "正常";
                        }else if(data.status=="2"){
                            return "已拦截";
                        }else{
                            return "已删除";
                        }
                    }},
                {title:"操作",align:"center",templet:function (data) {
                        if(data.status=="1"){
                            return "<div>"
                                +"<a href='javascript:chakan2("+data.id+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                +"<a href='javascript:lanjie("+data.id+");' style='color: #2F4056;margin-right: 10px;'>拦截</a>"
                                +"<a href='javascript:shanchu("+data.id+");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                +"</div>";
                        }else if(data.status=="2"){
                            return "<div>"
                                + "<a href='javascript:chakan2(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "<a href='javascript:shanchu(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                + "</div>";
                        }else if(data.status=="3"){
                            return "<div>"
                                + "<a href='javascript:chakan2(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "</div>";
                        }
                    }}
            ]]
        })
    };
    /*查看*/
    chakan2 = function chakan2(id){
        //console.log(courseId);
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"id":id,"type":2},
            url:"/courseComment/getUserComment",
            success:function (data) {
                UserLayer(data.data);
            }
        });
    };
    function UserLayer(data){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看课程评论",
            content:"/newcomm/comment-course-see2.html",
            area:["800px","600px"],
            btn:["关闭"],
            yes:function(index){
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                localStorage.setItem("commodityId", data.commodityId);
                obj.find("#artcleTitle").text(data.artcleTitle);
                obj.find("#createDate").text(data.createDate);
                obj.find("#trueName").text(data.trueName);
                obj.find("#courseNprice").text(data.courseNprice);
                obj.find("#coursePrimary").text(data.coursePrimary);
                obj.find("#title").text(data.title);
                obj.find("#recommend").text(data.recommend);
                if(data.courseCstatus=="2"){
                    var courseStatus = "上架";
                }else {
                    var courseStatus =  "未上架";
                }
                obj.find("#courseStatus").text(courseStatus);

                if(data.courseCstatus=="3"){
                    var courseCstatus = "未审核";
                }else if(data.courseCstatus=="4"){
                    var courseCstatus = "已通过";
                }else if(data.courseCstatus=="5"){
                    var courseCstatus = "审核中";
                }else{
                    var courseCstatus = "审核失败";
                }

                obj.find("#courseCstatus").text(courseCstatus);
                obj.find("#courseTime").text(data.courseTime);
                obj.find("#likeNum").text(data.likeNum);
                obj.find("#shareNum").text(data.shareNum);
                obj.find("#collectNum").text(data.collectNum);
                obj.find("#lookNum").text(data.lookNum);
                obj.find("#saleNum").text(data.saleNum);
                obj.find("#courseUrl").attr("src", 'http://'+data.courseUrl);
                if(data.courseInfo==null){
                    var courseInfo = "";
                }else{
                    var courseInfo = data.courseInfo;
                }
                obj.find("#courseInfo").text(courseInfo);
                obj.find("#userId").val(userId);
                obj.find("#trueNamee").val(userName);
                obj.find("#account").val(account);
            }
        })
    };
    /*前端用户*/
    $("#qianduan").click(function () {
        location.reload();
    });
    /*管理人员*/
    $("#guanli").click(function () {
        show("guanli");
        end("qianduan");
        show("quanbu");
        end("zhengchang");
        end("yilanjie");
        end("yishanchu");
        count = 0;
        flag = false;
        getAfterTable("/courseComment/getAfterTable?status="+status+"&type="+2);
        numCount("");
        numCountAll("1",$("#span2"));
        numCountAll("2",$("#span3"));
        numCountAll("3",$("#span4"));
    })

    /*显示*/
    function show(elementId) {
        var obj1 = document.getElementById(elementId);
        obj1.style.backgroundColor="#2F4056";
        obj1.style.color="#FFFFFF";
    }
    /*不显示*/
    function end(elementId) {
        var obj2 = document.getElementById(elementId);
        obj2.style.backgroundColor="#F9F9F9";
        obj2.style.color="#666666";
        obj2.style.border="1px solid #D2D2D2";
    }
    /*获取一个时间*/
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today);
        return nowTime;
    }
    /*时间的补零操作*/
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    }
    /*刷新*/
    $('#shuaxin').click(function () {
        location.reload();
    })
    /*返回*/
    $("#goBack").click(function (s) {
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    })
});